package top.jacktgq.concurrent_utils.pool;

import top.jacktgq.utils.LogUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @Author CandyWall
 * @Date 2020/11/4--18:24
 * @Description 解决饥饿问题，处理不同的问题，创建不同的线程池
 */
public class StarvationProblem_Solve {
    private static final List<String> MENU = Arrays.asList("地三鲜", "宫保鸡丁", "辣子鸡丁", "烤鸡腿");
    private static Random random = new Random();
    private static String cooking() {
        return MENU.get(random.nextInt(MENU.size()));
    }
    public static void main(String[] args) {
        ExecutorService waiterPool = Executors.newFixedThreadPool(1);
        ExecutorService cookPool = Executors.newFixedThreadPool(1);
        waiterPool.execute(() -> {
            LogUtils.log("StarvationProblem", "处理点餐...");
            Future<String> dish = cookPool.submit(() -> {
                LogUtils.log("StarvationProblem", "做菜...");
                return cooking();
            });
            try {
                LogUtils.log("StarvationProblem", "上菜：" + dish.get());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        waiterPool.execute(() -> {
            LogUtils.log("StarvationProblem", "处理点餐...");
            Future<String> dish = cookPool.submit(() -> {
                LogUtils.log("StarvationProblem", "做菜...");
                return cooking();
            });
            try {
                LogUtils.log("StarvationProblem", "上菜：" + dish.get());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
